//
//  SuanMaViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/8.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "SuanMaViewController.h"
#import "CommonData.h"
#import <Masonry.h>
#import "SuanMaTableViewCell.h"
#import "SuanMaCenterTableViewCell.h"
#import "KuangJiShouYiViewController.h"
#import "taskOverviewService.h"
#import "dataOverviewMinerList.h"
#import "HomePassWordView.h"

@interface SuanMaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)    HomePassWordView*   viewPassword;
@property (nonatomic,strong)    UITableView *tableView;
@property (nonatomic,strong)    dataOverviewMinerListInfo   *modelData;
///选择算码model
@property (nonatomic,strong)    dataOverviewUserGteMinerList *mModel;
@end

@implementation SuanMaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的算码";
    self.mModel = [dataOverviewUserGteMinerList new];
    
    [self tableView];
    [self loadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    if (self.navigationController.navigationBar.hidden == YES) {
        self.navigationController.navigationBar.hidden = NO;
    }
    self.hidesBottomBarWhenPushed = YES;
    if (self.tabBarController.tabBar.hidden == NO) {
        self.tabBarController.tabBar.hidden = YES;
    }
    [self setBlackStatus:YES];
}

-(void)goBack:(id)sender{
    [self goRootBack:2];
}



-(HomePassWordView*)viewPassword{
    if (!_viewPassword ){
        _viewPassword = [[[NSBundle mainBundle] loadNibNamed:@"HomePassWordView" owner:nil options:nil] objectAtIndex:0];
        [_viewPassword initUI];
        [self.view addSubview:_viewPassword];
        _viewPassword.hidden = YES;
        [_viewPassword mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.mas_equalTo(self.view);
            make.top.offset(60);
            make.bottom.mas_equalTo(self.view).offset(0);
        }];
        [_viewPassword.btnNext setTitle:@"同意支付" forState:UIControlStateNormal];
    }
    return _viewPassword;
}

#pragma mark - loadData

-(void)loadData{
    [DKProgressHUD showLoading];
    [taskOverviewService getUserMinerList:nil success:^(dataOverviewMinerList *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            self.modelData = response.data;
            [self.tableView reloadData];
        }else{
            [DKProgressHUD showErrorWithStatus:response.statusDesc];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}



#pragma mark - tableivew
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.top.and.right.mas_equalTo(self.view);
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        }];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        //定义显示默认cell的横线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
    }
    return _tableView;
}

#pragma mark - UITableViewDelegate

//定义每一个section有多少row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return [self.modelData.userGteMinerList count];
    }
    return 1;
}

//定义有多少section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

//定义CellRow的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 180;
    }else if(indexPath.section == 1){
        return 326;
    }
    return 0;
}

//定义section的Footer的view视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = COLOR_Background;
    return view;
}

//定义section的Footer的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}


//定义section的header的view视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = COLOR_Background;
    return view;
}

//定义section的header的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

//主体cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
        {
            SuanMaTableViewCell *cell = [SuanMaTableViewCell cellWithTableView:tableView IndexPath:indexPath];
            dataOverviewUserGteMinerList *mm =[self.modelData.userGteMinerList objectAtIndex:indexPath.row];
            dataOverviewUserGteMinerList *mModel = [dataOverviewUserGteMinerList mj_objectWithKeyValues:mm];
            cell.labelNo.text = mModel.minerNo;
            cell.labelDsl.text = mModel.unitEfficiency;
            cell.labelSYsl.text = mModel.remainEfficiency;
            cell.labelTitle.text = mModel.name;
            cell.labelStatus.text = mModel.status;
            cell.btnSMyj.hidden = YES;
            if (mModel.needDeposit || mModel.canReturnDeposit) {
                cell.btnSMyj.hidden = NO;
                cell.btnSMyj.tag = indexPath.row;
                if (self.mModel.needDeposit) {
                    [cell.btnSMyj setTitle:@"算码押金" forState:UIControlStateNormal];
                }else if(self.mModel.canReturnDeposit){
                    [cell.btnSMyj setTitle:@"退还押金" forState:UIControlStateNormal];
                }
                [cell.btnSMyj addTarget:self action:@selector(btnSMyjClick:) forControlEvents:UIControlEventTouchUpInside];
            }
            return cell;
        }
            break;
        case 1:{
            SuanMaCenterTableViewCell *cell = [SuanMaCenterTableViewCell cellWithTableView:tableView IndexPath:indexPath];
            [cell.btnKuangJi addTarget:self action:@selector(btnKuangJiClick:) forControlEvents:UIControlEventTouchUpInside];
            cell.labelBiNumber.text = self.modelData.profit;
            cell.labeilLeiJi.text = [NSString stringWithFormat:@"累计获得：%@ GTE",self.modelData.totalProfit];//累计获得：420.325695 GTE
            cell.labelSuanLi.text = [NSString stringWithFormat:@"当前算力：%@ GTE/天",self.modelData.currEfficiency];//当前算力：0.01GTE/S
            NSString *strBtn = [NSString stringWithFormat:@"总算码数量：%@台",self.modelData.minerNumber];
            [cell.btnKuangJi setTitle:strBtn forState:UIControlStateNormal];
            return cell;
        }
            break;
        default:
            break;
    }
    return  [UITableViewCell new];
}

///算力押金按钮点击事件
-(void)btnSMyjClick:(UIButton*)sender{
    
    dataOverviewUserGteMinerList *mm =[self.modelData.userGteMinerList objectAtIndex:sender.tag];
    self.mModel = [dataOverviewUserGteMinerList mj_objectWithKeyValues:mm];
    
    if (self.mModel.needDeposit) {
        self.viewPassword.labelTitle.text = [NSString stringWithFormat:@"支付算码押金%@GTE",self.mModel.deposit];
    }else if(self.mModel.canReturnDeposit){
        self.viewPassword.labelTitle.text = [NSString stringWithFormat:@"退回算码押金%@GTE",self.mModel.deposit];
    }else{
        return;
    }
    
    
    ///进入输入密码的模块
    self.viewPassword.hidden = NO;
    __block SuanMaViewController *  blockSelf = self;
    self.viewPassword.blockSubmit = ^(NSString *pwd) {
        if (blockSelf.mModel.needDeposit) {
            
            NSMutableDictionary *dict = [NSMutableDictionary new];
            [dict setValue:@(blockSelf.mModel.pid) forKey:@"minerId"];
            [dict setValue:blockSelf.mModel.deposit forKey:@"deposit"];
            [dict setValue:pwd forKey:@"payPassword"];
            [DKProgressHUD showLoading];
            [taskOverviewService getUserMinerPayYJ:dict success:^(BaseModel *response, BOOL success) {
                [DKProgressHUD dismiss];
                if (success) {
                    blockSelf.viewPassword.hidden = YES;
                    [blockSelf loadData];
                }else{
                    [DKProgressHUD showErrorWithStatus:response.statusDesc];
                }
            } failture:^(NSError *error, NSString *strDescription) {
                [DKProgressHUD dismiss];
                [DKProgressHUD showErrorWithStatus:strDescription];
            }];
        }else if(blockSelf.mModel.canReturnDeposit){
            NSMutableDictionary *dict = [NSMutableDictionary new];
            [dict setValue:@(blockSelf.mModel.pid) forKey:@"minerId"];
            [dict setValue:pwd forKey:@"payPassword"];
            [DKProgressHUD showLoading];
            [taskOverviewService getUserMinerReturnYJ: dict success:^(BaseModel *response, BOOL success) {
                [DKProgressHUD dismiss];
                if (success) {
                    blockSelf.viewPassword.hidden = YES;
                    [blockSelf loadData];
                }else{
                    [DKProgressHUD showErrorWithStatus:response.statusDesc];
                }
            } failture:^(NSError *error, NSString *strDescription) {
                [DKProgressHUD dismiss];
                [DKProgressHUD showErrorWithStatus:strDescription];
            }];
        }
    };
}

///查询矿机列表
-(void)btnKuangJiClick:(UIButton*)sender{
    [self.navigationController pushViewController:[KuangJiShouYiViewController new] animated:YES];
}


@end
